﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ZG_member.DAL
{
    public class Speak
    {
        public bool add(ZG_member.Model.Speak model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Speak(");
            strSql.Append("Title,Contents)");
            strSql.Append("values (");
            strSql.Append("@Title,@Contents)");
            SqlParameter[] parameters = {
					 
					new SqlParameter("@Title", SqlDbType.VarChar,50),
					new SqlParameter("@Contents", SqlDbType.VarChar,5000)};

            parameters[0].Value = model.Title;
            parameters[1].Value = model.Contents;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public DataSet GetListWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM Speak");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(string title,string content)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Speak set ");
            strSql.Append("Contents=@Contents");
            strSql.Append(" where Title=@Title ");
            SqlParameter[] parameters = {
                                            new SqlParameter("@Title", SqlDbType.VarChar,50),
                                            new SqlParameter("@Contents", SqlDbType.VarChar,100)
					                        };
            parameters[0].Value = title;
            parameters[1].Value = content;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return DbHelperSQL.Query(strWhere.ToString());
        }


        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Speak ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public ZG_member.Model.Speak GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Title,Contents from Speak ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            ZG_member.Model.Speak model = new ZG_member.Model.Speak();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Title"] != null && ds.Tables[0].Rows[0]["Title"].ToString() != "")
                {
                    model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Contents"] != null && ds.Tables[0].Rows[0]["Contents"].ToString() != "")
                {
                    model.Contents = ds.Tables[0].Rows[0]["Contents"].ToString();
                }
               
                return model;
            }
            else
            {
                return null;
            }
        }


        public ZG_member.Model.Speak GetModel(string TitleText)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Title,Contents from Speak ");
            strSql.Append(" where Title=@TitleText");
            SqlParameter[] parameters = {
					new SqlParameter("@TitleText", SqlDbType.VarChar,8)
};
            parameters[0].Value = TitleText;

            ZG_member.Model.Speak model = new ZG_member.Model.Speak();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Title"] != null && ds.Tables[0].Rows[0]["Title"].ToString() != "")
                {
                    model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Contents"] != null && ds.Tables[0].Rows[0]["Contents"].ToString() != "")
                {
                    model.Contents = ds.Tables[0].Rows[0]["Contents"].ToString();
                }

                return model;
            }
            else
            {
                return null;
            }
        }




    }
}
